﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SRM145_2
{
    class Program
    {
        static void Main(string[] args)
        {
            Verify("BW", new string[]
                            {"AAAAAAAA",
                            "ABWBWBWA",
                            "AWBWBWBA",
                            "ABWBWBWA",
                            "AWBWBWBA",
                            "AAAAAAAA"}, 24);

            Verify("BW", new string[]
                            {"BBBBBBBB",
                             "BBWBWBWB",
                             "BWBWBWBB",
                             "BBWBWBWB",
                             "BWBWBWBB",
                             "BBBBBBBB"}, 48);

            Verify("ACEGIKMOQSUWY", new string[]
                                        {"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX",
                                         "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX",
                                         "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX",
                                         "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX",
                                         "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX",
                                         "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX"}, 150);

            Verify("CA", new string[]
                            {"BBBBBBB",
                             "BBBBBBB",
                             "BBBBBBB"}, 0);

            Verify("DCBA", new string[] { "ACBD" }, 4);

            Console.ReadKey();
        }

        private static void Verify(string p1, string[] p2, int p3)
        {
            Console.WriteLine(new ImageDithering().count(p1, p2) == p3);
        }
    }


    public class ImageDithering
    {
        public int count(string dithered, string[] screen)
        {
            bool[] map = new bool[26];
            for (int i = 0; i < dithered.Length; i++)
                map[(int)(dithered[i] - 65)] = true;

            int count = 0;

            for (int i = 0; i < screen.Length; i++)
            {
                for (int j = 0; j < screen[i].Length; j++)
                {
                    if (map[(int)(screen[i][j]) - 65])
                        count++;
                }
            }

            return count;
        }
    }
}
